package com.icebergteam.warfacewidget.data.sql;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import com.icebergteam.warfacewidget.AppManager;
import com.icebergteam.warfacewidget.core.LogManager;
import com.icebergteam.warfacewidget.core.utils.ViewUtils;
import com.icebergteam.warfacewidget.rank.data.TopUsersItem;

public class UserRankTable extends BasicTable{

	public static String className = UserRankTable.class.getCanonicalName();
	// Constants
	// Table
	public static final String TABLE_NAME	= "user_rank";
	// Fields
	public static final String FIELD_RANK			= "rank ";
	public static final String FIELD_NAME			= "html ";
	public static final String FIELD_USER_RATING = "user_rating ";
	public static final String FIELD_MONTH_RATING = "month_rating ";
	public static final String FIELD_CLANS_RATING = "top_clans_rating ";
	public static final String FIELD_SERVER = "server ";
	public static final String FIELD_ID = "id ";
	public static final String FIELD_PUBDATE = "pubdate ";
	
	public UserRankTable() {

	}
	
	public String createTableStr() {
		LogManager.LogV(className, "createTable");
	
		String field_id 	= FIELD_ID	+ "integer";
		String field_rank		= FIELD_RANK		+ "integer";
		String field_name		= FIELD_NAME	+ "text";
		String field_user_rating 	= FIELD_USER_RATING	+ "integer";
		String field_month_rating 	= FIELD_MONTH_RATING	+ "integer";
		String field_top_clans_rating 	= FIELD_CLANS_RATING	+ "integer";
		String field_server 	= FIELD_SERVER	+ "integer";
		String field_pubdate 	= FIELD_PUBDATE	+ "integer";
					
		return AppManager.getDatabase().createTableStr(TABLE_NAME, field_id, field_name, field_rank, field_user_rating, field_month_rating, field_top_clans_rating, field_server, field_pubdate);
	
	}
	
	public boolean existsUser(int id, int server) throws SQLException{
		try {
			return (AppManager.getDatabase().getUserRankTable().getCount(String.format("%s = '%s' AND %s = '%s'",UserRankTable.FIELD_ID, UserRankTable.FIELD_SERVER)) >0)? true: false;
		} catch (Exception e) {
			new SQLException(e.getLocalizedMessage());
			return false;
			
		}
		
	}
	
	public long minDate(int server) throws SQLException{
		
			SQLiteDatabase db = AppManager.getDatabase().getWritableDatabase();
			if(db != null && db.isOpen()){
				Cursor cursor = db.rawQuery(String.format("select min(%s) as min_date from %s", UserRankTable.FIELD_PUBDATE,  TABLE_NAME), null);  
				cursor.moveToFirst();
				cursor.getLong(0);
			}
			return -1;
		
		
	}

//	
//	public Cursor getRank() throws SQLException{
//		try {
//			SQLiteDatabase db = AppManager.getDatabase().getWritableDatabase();
//			if(db != null && db.isOpen()){
//				String mask = "";
//				
//				mask+=String.format("%s = '%s'", UserRankTable.FIELD_NAME, NetworkManager.getUri(new RSSFeedService(), false));
//				LogManager.LogV(UserRankTable.className, LogManager.getExecuteMethodName(), mask);
//				return db.query(
//					UserRankTable.TABLE_NAME, 
//					new String[] { UserRankTable.FIELD_URL, UserRankTable.FIELD_HTML, UserRankTable.FIELD_USER_RATING}, 
//					mask,
//					null,
//					null, 
//					null,
//					String.format(" limit %s", AppManager.getNewsSettings().getCountViewNews())
//					);
//			}
//		} catch (Exception e) {
//			LogManager.LogEA(UserRankTable.className, ViewUtils.getErrorMessage(e));
//		}
//		return null;
//	}
	
	public Cursor getRankByServer(int server) throws SQLException{
	try {
		SQLiteDatabase db = AppManager.getDatabase().getWritableDatabase();
		if(db != null && db.isOpen()){
			String mask = "";
			
			mask+=String.format("%s = %s", UserRankTable.FIELD_SERVER, server);
			LogManager.LogV(UserRankTable.className, LogManager.getExecuteMethodName(), mask);
			return db.query(
				UserRankTable.TABLE_NAME, 
				new String[] { UserRankTable.FIELD_ID, UserRankTable.FIELD_NAME, UserRankTable.FIELD_RANK, UserRankTable.FIELD_USER_RATING, UserRankTable.FIELD_CLANS_RATING, UserRankTable.FIELD_MONTH_RATING, UserRankTable.FIELD_SERVER}, 
				mask,
				null,
				null, 
				null,
				null
				);
		}
	} catch (Exception e) {
		LogManager.LogEA(UserRankTable.className, ViewUtils.getErrorMessage(e));
	}
	return null;
}
//	
		
//	public Cursor getRankByUrl(String url) throws SQLException{
//		try {
//			SQLiteDatabase db = AppManager.getDatabase().getWritableDatabase();
//			if(db != null && db.isOpen()){
//				return db.query(
//					UserRankTable.TABLE_NAME, 
//					new String[] { UserRankTable.FIELD_URL, UserRankTable.FIELD_HTML, UserRankTable.FIELD_USER_RATING}, 
//					String.format("%s = ?", UserRankTable.FIELD_URL), 
//					new String[]{url}, 
//					null, 
//					null,
//					null
//					);
//			}
//		} catch (Exception e) {
//			LogManager.LogEA(UserRankTable.className, ViewUtils.getErrorMessage(e));
//		}
//		return null;
//	}
//	public long getDate(String url){
//		try {
//			SQLiteDatabase db = AppManager.getDatabase().getWritableDatabase();
//			if(db != null && db.isOpen()){
//				Cursor cursor = db.query(
//					UserRankTable.TABLE_NAME, 
//					new String[] { UserRankTable.FIELD_USER_RATING}, 
//					String.format("%s = ?", UserRankTable.FIELD_URL), 
//					new String[]{url}, 
//					null, 
//					null,
//					null
//					);
//				
//				cursor.moveToFirst();
//				return cursor.getLong(0);
//				
//			}
//		} catch (Exception e) {
//			LogManager.LogEA(UserRankTable.className, ViewUtils.getErrorMessage(e));
//		}
//		return -1;
//	}
	
	public void removeUser(int id, int server) throws SQLException{
		SQLiteDatabase db = AppManager.getDatabase().getWritableDatabase();
		if(db != null && db.isOpen()){
					
			db.delete(NewsTable.TABLE_NAME, String.format("%s = '%s' AND %s = '%s'", UserRankTable.FIELD_ID, id, UserRankTable.FIELD_SERVER, server), null);
		}
	}
	
	
	@Override
	protected WhereWrapper getUpdateWhere(ContentValues cv) {
		WhereWrapper where = new WhereWrapper();
		where.setWhere(String.format("%s=? AND %s=?", UserRankTable.FIELD_NAME, UserRankTable.FIELD_SERVER));
		where.addValue(cv.getAsString(UserRankTable.FIELD_NAME));
		where.addValue(cv.getAsString(UserRankTable.FIELD_SERVER));
		return where;
	}
	
	@Override
	protected String getTableName() {
		return TABLE_NAME;
	}
	
	public ArrayList<TopUsersItem> getTopUsersList(int server) throws SQLException, Exception{
		ArrayList<TopUsersItem> listTopUsers = new ArrayList<TopUsersItem>();
		
		Cursor cursor = AppManager.getDatabase().getUserRankTable().getRankByServer(server);
		
		while(cursor.moveToNext()){
			TopUsersItem item = new TopUsersItem();
			item.setId(cursor.getInt(cursor.getColumnIndex(UserRankTable.FIELD_ID.trim())));
			item.setName(cursor.getString(cursor.getColumnIndex(UserRankTable.FIELD_NAME.trim())));
			item.setRank(cursor.getInt(cursor.getColumnIndex(UserRankTable.FIELD_RANK.trim())));
			item.setUserRating(cursor.getInt(cursor.getColumnIndex(UserRankTable.FIELD_USER_RATING.trim())));
			item.setMonthRating(cursor.getInt(cursor.getColumnIndex(UserRankTable.FIELD_MONTH_RATING.trim())));
			item.setClansRating(cursor.getInt(cursor.getColumnIndex(UserRankTable.FIELD_CLANS_RATING.trim())));
			item.setServer(cursor.getInt(cursor.getColumnIndex(UserRankTable.FIELD_SERVER.trim())));
			
			listTopUsers.add(item);
		}
		return listTopUsers;
	}
}